<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>回收站</el-breadcrumb-item>
      <el-breadcrumb-item>删除列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="30">
        <el-col :span="7">
          <el-input
            @clear="getQuestionnaireList"
            clearable
            placeholder="请输入内容"
            v-model="queryInfo.query"
          >
            <el-button @click="getQuestionnaireList" slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="17">
          <div class="btns">
            <el-button type="danger" @click="deleteAll()">
              <i class="el-icon-delete"></i>彻底清空
            </el-button>
          </div>
        </el-col>
      </el-row>
      <!-- 用户列表区域 -->
      <el-table
        :data="questionnairelist"
        style="width: 100%"
        border
        stripe
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left">
              <el-form-item label="创建时间：">
                <span>{{ props.row.addtime }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column type="selection"></el-table-column>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column sortable prop="title" label="问卷名称"></el-table-column>
        <el-table-column sortable prop="status" label="问卷状态"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- 恢复问卷按钮 -->
            <el-tooltip effect="dark" content="恢复" placement="top">
              <el-button
                :enterable="false"
                type="success"
                icon="el-icon-refresh"
                size="small"
                circle
                @click="recover(scope.row)"
              ></el-button>
            </el-tooltip>
            <!-- 彻底删除按钮 -->
            <el-tooltip effect="dark" content="彻底删除" placement="top">
              <el-button
                :enterable="false"
                type="danger"
                icon="el-icon-delete"
                size="small"
                circle
                @click="deleteSingle(scope.row)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 获取问卷列表的参数
      queryInfo: {
        query: '',
        // 当前的页数
        pagenum: 1,
        // 当前每页显示多少条数据
        pagesize: 5,
        // 获取回收站的问卷列表
        type: 2
      },
      questionnairelist: [],
      total: 0,
      // 多选的数据
      multipleSelection: [],
      // 存放删除的数据
      delarr: []
    }
  },
  created() {
    this.getQuestionnaireList()
  },
  methods: {
    async getQuestionnaireList() {
      const { data: res } = await this.$http.get('questionnaire', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取问卷列表失败！')
      }
      // this.$message.success('获取问卷列表成功！')
      this.questionnairelist = res.data.questionnaires
      this.total = res.data.totalpage
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      // console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getQuestionnaireList()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      // console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getQuestionnaireList()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 删除单个问卷
    deleteSingle(val) {
      this.delarr.push(val.questionnaireid)
      this.deleteQusetionnaire()
    },
    // 批量删除问卷
    deleteAll() {
      const length = this.multipleSelection.length
      if (length === 0) {
        return this.$message.info('请选择要删除的问卷')
      }
      for (let i = 0; i < length; i++) {
        this.delarr.push(this.multipleSelection[i].questionnaireid)
      }
      // console.log(this.delarr)
      this.deleteQusetionnaire()
    },
    async deleteQusetionnaire() {
      // 弹框是否询问删除问卷信息
      const confirmResult = await this.$confirm(
        '此操作将永久删除被选择的问卷, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      // 如果用户确认删除，则返回值为字符串 confirm
      // 如果用户取消了删除，则返回值为字符串 cancel
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      var sendData = {}
      var selectedText = this.delarr
      this.delarr = []
      sendData.questionnaireid = selectedText
      sendData.type = 2
      var sendJson = JSON.stringify(sendData)
      const { data: res } = await this.$http({
        method: 'delete',
        url: 'questionnaire',
        data: sendJson
      })
      if (res.meta.status !== 200) return this.$message.error('删除问卷失败')
      this.$message.success('删除问卷成功')
      this.getQuestionnaireList()
    },
    async recover(val) {
      var data = {}
      data.questionnaireid = val.questionnaireid
      var { data: res } = await this.$http.post('recover', data)
      if (res.meta.status !== 200) {
        this.$message.error('恢复问卷失败')
        return
      }
      this.$message.success('恢复问卷成功')
      this.getQuestionnaireList()
    }
  }
}
</script>

<style lang="less" scoped>
.el-pagination {
  margin-top: 30px;
}
.btns {
  display: inline-block;
  padding-right: 10px;
}
</style>
